package com.taotao.order.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taotao.common.utils.CookieUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbUser;

@Controller
public class OrderController {
	@Value("${CART_COOKIR_KEY}")
	private String CART_COOKIR_KEY;
	@RequestMapping("order/order-cart")
	public String showOrderCart(HttpServletRequest request){
		//取用户id
		TbUser user = (TbUser) request.getAttribute("user");
		//从cookie中取token，然后根据token查询用户信息。需要调用sso系统的服务。
		//根据用户id查询收货地址列表
		//取出购物的商品列表
		List<TbItem> cartList = getCartList(request);
		//将商品列表存入requset中
		request.setAttribute("cartList", cartList);
		return "order-cart";
	}
	public List<TbItem> getCartList(HttpServletRequest request){
		String value = CookieUtils.getCookieValue(request, CART_COOKIR_KEY, true);
		if (StringUtils.isBlank(value)) {
			return new ArrayList<>();
		}
		List<TbItem> list = JsonUtils.jsonToList(value, TbItem.class);
		return list;
	}
}
